<!--npm install mavon-editor --save-->
<template>
  <section class="orderBox">
    <el-form :inline="true" :model="searchForm" :rules="activityRules" ref="searchForm1" class="toolbar">
      <el-row>
        <el-col :span="7">
          <el-form-item label="活动专题" prop="activitySubject">
            <el-select v-model="searchForm.subject" placeholder="请选择"  clearable>
              <el-option v-for="item in activitySubject" :label="item.title" :key="item.id" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="活动主题" prop="title">
            <el-input v-model="searchForm.title"  ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker v-model="searchForm.startTime" type="datetime"
                            :picker-options="pickerOptions1" :editable="false" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="结束时间" prop="endTime" >
            <el-date-picker v-model="searchForm.endTime" type="datetime" :picker-options="pickerOptions2"
                            :editable="false" placeholder="选择日期时间" >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>

      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="活动要求">
            <el-input type="textarea" v-model="searchForm.required" :rows="7" style="width: 500px;" ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    <div class="divid-line"></div>
  <div id="app">
    <mavon-editor ref=md @imgAdd="$imgAdd" @imgDel="$imgDel" v-model="searchForm.content" placeholder="填写活动内容" ></mavon-editor>
  </div>
      <div class="divid-line"></div>
      <el-row>
        <div class="one-title">图片上传</div>
        <el-upload
          action="api/check/registerOrginzerCheck"
          :data = "upLoadData"
          name = "file"
          list-type="picture-card"
          :file-list="picList"
          :on-preview="handlePictureCardPreview"
          :on-success="uploadSuccess"
          :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-row>
      <div class="divid-line"></div>
      <el-row>
        <div class="one-title">附件上传</div>
        <el-upload
          class="upload-demo"
          ref="upload"
          name="file"
          :data = "upLoadData"
          action="api/check/registerOrginzerCheck"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="fileList"
          :auto-upload="false">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">开始上传</el-button>
          <div slot="tip" class="el-upload__tip"></div>
        </el-upload>
      </el-row>
      <div class="divid-line"></div>
    <div>
      <el-row>
        <el-col :offset="10">
          <el-form-item>
            <el-button @click="returnList" type="info">返回</el-button>
          </el-form-item>
          <el-form-item>
            <el-button @click="publishActivity('searchForm1')" type="success">确定</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </div>

    </el-form>
  </section>
</template>
<script>
import enums from '../../common/Enum'
import DataUtil from '../../common/dataUtil'
import Config from '../../common/config'
import ElRow from "element-ui/packages/row/src/row";
export default {
  data () {
    let compareDate = (time, form, txt, type) => {
//      let _cp1 = time.getTime() >= Date.now()
      let _cp2 = false
      let _input = this[form][txt]
      if (_input) {
        let _time = Date.parse(new Date(_input))
        if ((type == "s" && time.getTime() > _time) || (type == "e" && time.getTime() < _time)) {
          _cp2 = true
        }
      }
      return  _cp2
    };

    let activitySubjectValitor = (rule, value, callback) => {
      if (this.searchForm.subject === 0 || this.searchForm.subject == '') {
        callback(new Error('请选择活动专题！'));
      } else {
        callback();
      }
    };
    let timeValitor = (rule, value, callback) => {
      if (value === 0 || value == '') {
        callback(new Error('请选择时间！'));
      } else {
        callback();
      }
    };
    return {
      showDetail: true,
      subfield: true,
      code_style: 'solarized-dark',
      external_link: {
        hljs_css: function(css) {
          // 这是你的代码高亮配色文件路径
          return '/highlight.js-9.12.0/styles/' + css + '.min.css';
        },
        katex_css: false, // `false`表示禁用自动加载，它也可以是个函数，如果它是个函数，那么这个函数应该返回一个可访问的`katex`的css路径字符串
        // 我们没有设置`katex_js`, `hljs_js`, `hljs_lang`, `markdown_css`， `mavon-editor`会认为它的值为`true`，它会默认使用`cdnjs`相关外链加载
      },
      searchForm:{
        activitySubject: '',
        title:'',
        persionCount:0,
        attendanceCount:0,
        required:'',
        content:'',
        startTime:'',
        endTime:'',
        subject:'',
        requireCount:''
      },
      pickerOptions2:{
        disabledDate(time) {
          return compareDate(time, "searchForm", "startTime", "e")
        }
      },
      pickerOptions1: {
        disabledDate(time) {
          return compareDate(time, "searchForm", "endTime", "s")
        }
      },
      activitySubject: [],
      pageIndex: 1,
      pageSize: 10,

      activityRules: {
        activitySubject: [
          { required: true, validator: activitySubjectValitor, trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请填写主题！', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '活动内容不能为空！', trigger: 'blur' }
        ],
        startTime: [
          { required: true, validator: timeValitor, trigger: 'blur' }
        ],
        endTime: [
          { required: true, validator: timeValitor, trigger: 'blur' }
        ]
      },
      dialogImageUrl: '',
      dialogVisible: false,
      upLoadData: {
        userId: DataUtil.id(),
        type: 2
      },
      fileName:"",
      fileList:[

      ],
    }
  },
  methods: {
    // 绑定@imgAdd event
    $imgAdd(pos, $file){
        //todo:
      // 第一步.将图片上传到服务器.
//      var formdata = new FormData();
//      formdata.append('file', $file);
//      console.info(formdata);
//      this.$http({
//        method:'post',
//        url:'file/upload',
//        data:formdata,
//        headers: { 'Content-Type': 'multipart/form-data' },
//
//      }).then(response => {
//
//      });
//      var _data = {
//        data: formdata,
//        headers: { 'Content-Type': 'multipart/form-data' },
//      }
//      this.$http.post('file/upload',_data).then(response => {
//          if (response.body.code == 1) {
//            $vm.$img2Url(pos, response.body.data);
//          }
//      })
    },
    $imgDel(pos, $file){
      //todo:
    },
    getdate(dates) {
      if(!dates){
        return '';
      }
      var myDate = new Date(dates);
      let minute=myDate.getMinutes();
      if(minute<10){
        minute="0"+minute;
      }
      let seconds=myDate.getSeconds()
      if(seconds<10){
        seconds="0"+seconds;
      }
      return myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+myDate.getDate()+" "+myDate.getHours()+":"+minute+":"+seconds
    },
   getStatus(type) {
     let re = '';
     this.status.forEach(obj => {
      if (obj.code == type) {
        re = obj.name;
        return
      }
    });
     return re
   },
     search() {
      this.bindGrid()
    },
    rowClick(row){
      window.open('#/OrderDetails?seen=true&userUuid='+row.userUuid+
        '&uuid='+row.uuid);
    },
    bindGrid() {
        let _data = {
//          id: DataUtil.uuid,
          id :this.$route.query['id']||'',
          pageNo:this.pageIndex,
          pageSize:this.pageSize
        };
        if (_data.id == '' || _data.id == 0) {
          return ;
        }
        this.$http.post('activity/listActivity', _data).then(response => {
          if (1 == response.body.code) {
            this.searchForm = response.body.data.list[0];
            if (this.searchForm.subject) {
                this.upLoadData.type = 3;
            }
          } else {
            this.$message.error(body.message);
          }
        }).catch((e) =>{
          this.gridLoading = false;
        })
      },
    getActivitySubject() {

      this.$http.post('activity/listActivitySubject',{}).then(response => {
        if (response.body.code == 1) {
          this.activitySubject = response.body.data;
        }
      });
    },
    publishActivity(formName) {
         this.$refs[formName].validate((valid) => {
             if (valid) {
               this.searchForm.startTime = this.getdate(this.searchForm.startTime);
               this.searchForm.endTime = this.getdate(this.searchForm.endTime);
               this.searchForm.organizer = DataUtil.id();
               let _data = Object.assign({
                 "id":this.$route.query['id']||'',
                 "type":this.$route.query['type']||'',
                 "activityTitle": this.searchForm.title,
                 "require": this.searchForm.required,
               },this.searchForm);
               this.$http.post('activity/insertOrUpdateActivity', _data).then(response => {
                 if (response.body.code == 1) {
                   this.$message.success('发布活动成功!');
                   window.close();
                   if (this.$route.query['type'] == 1) {
                     window.open('#/OrginzerTrain');
                   } else if (this.$route.query['type'] == 2) {
                       window.open('#/VolunteerTrain');
                   } else {
                       window.open('#/FundraisingTrain');
                   }
                 }
               });
             } else {
               this.$message.error('还有需要填写的信息！');
               return false;
             }
         })
    },
    returnList() {
        let type = this.$route.query['type'];
        window.close();

      if (type == 1) {
        window.open('#/OrginzerTrain');
        } else if (type == 2) {
        window.open('#/VolunteerTrain');
      } else {
        window.open('#/FundraisingTrain');
      }
    },
    handleRemove(file) {
      this.fileName = file.name;
      this.$http.post('check/deleteOrginzeData',{"fileName": this.fileName});
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    uploadSuccess(response) {
      if (response.code == 1) {
        this.fileName = response.data;
      }
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    getAllCheckData() {
      this.$http.post('check/getAllOrginzeData', {userId: DataUtil.id(), type: 1, fileType: 1}).then(response => {
        if (response.body.code == 1) {
          this.picList = response.body.data;
        }
      });
      this.$http.post('check/getAllOrginzeData', {userId: DataUtil.id(), type: 1, fileType: 2}).then(response => {
        if (response.body.code == 1) {
          this.fileList = response.body.data;
        }
      });
    },
    },

    mounted: function () {
      this.bindGrid();
      this.getActivitySubject();
      this.getAllCheckData();
}
}
</script>
<style>
  .reviewTable {
    width: 80%;
    border-spacing: 0;
    border: 1px solid gray;
    margin: 20px auto;
  }
  .reviewTable td {
    text-align: center;
    border: 1px solid gray;
    height: 40px;
    line-height: 40px;

    width: 50%;
    word-wrap:break-word;
  }
  .videos {
    width: 80%;
    margin: 20px auto;
  }
  /*侧边框布局*/
  .containers {
    width: 100%;
    /*height: 400px;*/
    display: flex;

  }
  .containers .asides {
    flex: 6;

  }
  .containers .mains {
    flex: 4;
    margin-left: 40px;
  }
  .other_remark {
    width: 100%;
  }
  .mains .smallRemark {
    width: 100%;
  }
  .asides table {
    border-spacing: 0;
    border: 1px solid #A9A9A9 ;
    border-radius: 2px;
  }
  .asides table td {
    border: .5px solid #efefef;
    word-wrap:break-word;
    line-height: 20px;
    padding: 5px;
  }
  table td img {
    width: 100%;
  }
  .aside_title {
    font-weight: 400;
    line-height: 40px;
    font-size: 16px;
  }
  .question_radio {
    padding-bottom: 30px;
    margin: 5px 10px;
  }
  .question_radio .radio_box {
    /*margin-bottom: -30px;*/
    line-height: 20px;
    float: right;
  }
  .addreBook {
    text-align: center;
    width: 40px;
  }
  /* 标题 */
  .main-title{
    font-size: 22px;
    margin-top: -15px;
    margin-bottom: 15px;
  }
  .one-title{
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 7px;
  }
  .two-title{
    font-size: 13px;
    color: #888;
    margin: 10px 0
  }
  .subtitle{
    font-size: 13px;
    color: #888;
  }
  .orderBox .el-row {
    margin: 8px 30px;
  }
  .orderBox .el-col span{
    margin-right: 10px;
  }
  .orderBox .el-button span,.orderBox .el-tag span{
    margin-right: 0;
  }
  .orderBox .box-card .el-button{
    margin-right: 5px;
  }
  .orderBox .grid-content {
    line-height: 28px;
  }

  .divid-line{
    height: 10px;
    background: #f1f2f7;
  }
  /*借款信息*/
  .loanDetail{
    margin: 0 30px 15px;
    width: 94%;
    max-height:250px;
  }
  /*依图识别*/
  .yituInfo{
    display: block;
    text-align: center;
    margin-top: 10px;
  }
  /*征信*/
  .orderBox .box-card{
    height: 225px;
  }
  .JXLBox{
    border: 1px solid #ccc;
    padding: 10px
  }
  .JXLBox div{
    padding: 5px;
  }

  .orderBox .el-table .info-row {
    background: #e2f0e4;
  }

  .orderBox .el-table .red-row{
    background: #F33;
  }
  .redInfo{
    color: #F33;
  }

  .line-height .el-col{
    margin-bottom: 10px;
  }
  .faceCheck {
    margin: 0 auto;
    overflow: hidden;
  }
  .faceCheck .faceImg{
    width: 45%;
    height: 350px;
    overflow: hidden;
    margin: 1.5%;
    float: left;
    text-align: center;
  }
  .faceCheck .faceImg img {
    margin: 0 auto;
    display: block;
    width: 90%;
    height: 90%;
    /*    border: .5px solid #F1F2F7;
    border-radius: 5px;*/
  }
  .faceImgTop img{
    margin: 0 auto;
    height: 200px;
    display: block;
    max-height: 100%;
    max-width: 100%;
    min-width: 65%;
    min-height: 65%;
    border: .5px solid #F1F2F7;
    border-radius: 5px;
  }
  .faceImgBottom img{
    margin: 0 auto;
    display: block;
    height: 300px;
    min-width: 80%;
    min-height: 80%;
    border: .5px solid #F1F2F7;
    border-radius: 5px;
  }
  table td img {
    width: 100%;
  }
  .faceImgTop .handleImg {
    width: 40%;
    transform: rotate(-90deg);
    -webkit-transform: rotate(-90deg);
  }
</style>


